package com.yilu.driver.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.is.common.utils.ScreenUtils;
import com.is.ui.banner.Banner;
import com.is.ui.banner.listener.OnBannerListener;
import com.is.ui.banner.loader.GlideImageLoader;
import com.is.ui.sortbutton.Interface.ViewControl;
import com.is.ui.sortbutton.SoreButton;
import com.is.ui.xrecyclerview.XRecyclerView;
import com.is.ui.xrecyclerview.universaladapter.ViewHolderHelper;
import com.is.ui.xrecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import com.yilu.driver.R;
import com.yilu.driver.base.BaseFragment;
import com.yilu.driver.entity.Weather;
import com.yilu.driver.mvp.contract.ProjectListContract;
import com.yilu.driver.ui.activity.TraineeMgmtActivity;
import com.yilu.driver.ui.adapter.SortButtonAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.yilu.driver.R.layout.project;


/**
 * @author Administrator
 * @date 2017/4/6 0006
 */

public class MainFragment extends BaseFragment implements
        ProjectListContract.View, OnBannerListener, ViewControl, XRecyclerView.LoadingListener {
    @Bind(R.id.irc)
    XRecyclerView irc;
    @Bind(R.id.ib_back)
    ImageButton ibBack;
    @Bind(R.id.txt_title)
    TextView txtTitle;
    private Banner banner;
    private CommonRecycleViewAdapter<String> commonRecycleViewAdapter;

    private String[] titles = {"角色权限管理", "学员管理", "接待管理", "服务管理", "车辆管理", "行政人力资源管理", "数据统计", "日志记录", "约车（教练）", "报考",
            "教学记录", "学习记录", "教考宝典", "约车（学员）", "约车（校长）"};

    private SoreButton sortButton;
    private List<Integer> list;

    /**
     * Create a new instance of the fragment
     */
    public static MainFragment newInstance(int index) {
        MainFragment fragment = new MainFragment();
        Bundle b = new Bundle();
        b.putInt("index", index);
        fragment.setArguments(b);
        return fragment;
    }


    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String msg) {
    }

    @Override
    public void showException(String msg) {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void OnBannerClick(int position) {
        showToast("点击了" + position);
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void initViewsAndEvents() {
        View header = LayoutInflater.from(mContext).inflate(R.layout.activity_head, null);
        banner = (Banner) header.findViewById(R.id.banner);
        sortButton = (SoreButton) header.findViewById(R.id.soreButton);
        //设置界面监听
        sortButton.setViewControl(this);
        //添加界面到list
        list = new ArrayList<>();
        list.add(R.layout.viewpager_page);
        list.add(R.layout.viewpager_page);
        //控件相关设置
        sortButton
                //设置选中和未选中指示器图标
                .setIndicator(R.drawable.radio_select, R.drawable.radio_unselected)
                //设置指示器半间距px
                .setDistance(10)
                //设置view组
                .setView(list)
                .init();
        banner.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.getScreenHeight(mContext) / 4));
        irc.addHeaderView(header);
        commonRecycleViewAdapter = new CommonRecycleViewAdapter<String>(mContext, project) {

            @Override
            public void convert(ViewHolderHelper helper, String s) {
                TextView txtName = helper.getView(R.id.txt_name);
                txtName.setText(s);
            }
        };
        irc.setAdapter(commonRecycleViewAdapter);
        irc.setLayoutManager(new LinearLayoutManager(mContext));
        irc.setLoadingListener(this);
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            data.add(i + "YiLu");
        }
        commonRecycleViewAdapter.addAll(data);
        commonRecycleViewAdapter.notifyDataSetChanged();
        List<String> url = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            url.add("http://img3.imgtn.bdimg.com/it/u=4271053251,2424464488&fm=23&gp=0.jpg");
        }
        banner.setImages(url)
                .setImageLoader(new GlideImageLoader())
                .setOnBannerListener(this)
                .start();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    /**
     * SortButton
     *
     * @param view
     * @param type
     */
    @Override
    public void setView(View view, final int type) {
        switch (type) {
            case 0://第一个界面
                GridView gridView = (GridView) view.findViewById(R.id.gridView);
                SortButtonAdapter adapter = new SortButtonAdapter(mContext, setData());
                gridView.setAdapter(adapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        readyGo((Class<?>) setData().get(position).get("class"));
                    }
                });
                break;
            case 1://第二个界面
                GridView gridView2 = (GridView) view.findViewById(R.id.gridView);
                SortButtonAdapter adapter2 = new SortButtonAdapter(mContext, setData2());
                gridView2.setAdapter(adapter2);
                gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(mContext, "第" + type + "页" + position, Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
    }

    /**
     * 第一页数据
     *
     * @return
     */
    private List<HashMap> setData() {
        List<HashMap> data = new ArrayList<>();
        HashMap hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_0);
        hashMap.put("name", titles[0]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_1);
        hashMap.put("name", titles[1]);
        hashMap.put("class", TraineeMgmtActivity.class);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_2);
        hashMap.put("name", titles[2]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_3);
        hashMap.put("name", titles[3]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_4);
        hashMap.put("name", titles[4]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_5);
        hashMap.put("name", titles[5]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_6);
        hashMap.put("name", titles[6]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_7);
        hashMap.put("name", titles[7]);
        data.add(hashMap);
        return data;
    }

    /**
     * 第二页数据
     *
     * @return
     */
    private List<HashMap> setData2() {
        List<HashMap> data = new ArrayList<>();
        HashMap hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_11);
        hashMap.put("name", titles[8]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_12);
        hashMap.put("name", titles[9]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_13);
        hashMap.put("name", titles[10]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_14);
        hashMap.put("name", titles[11]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_15);
        hashMap.put("name", titles[12]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_16);
        hashMap.put("name", titles[13]);
        data.add(hashMap);
        hashMap = new HashMap();
        hashMap.put("icon", R.drawable.ic_category_17);
        hashMap.put("name", titles[14]);
        data.add(hashMap);
        return data;
    }

    @Override
    public void returnWeather(Weather photoGirls) {
      //  commonRecycleViewAdapter.replaceAll(photoGirls.getResult());
      //  irc.refreshComplete();
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
